<?php
function exitJson($data)
{
    exit(json_encode($data,JSON_UNESCAPED_UNICODE));
}
function returnArr($code = '1', $msg = '成功', $data = [],$count=null)
{
    $res= array();
    $res['code'] = $code;
    if(is_array($msg)){
        $res['msg'] = join('<br/>',$msg);
    }else{
        $res['msg']=$msg;
    }
    $res['data'] = $data;
    if(isset($count)){
        $res['count'] = $count;
    }
    return $res;
}
function returnJson($code = '1', $msg = '成功', $data = [],$count=null){
    exitJson(returnArr($code, $msg, $data,$count));
}

/**
 * 模拟CURL请求
 * @param string $url 路径
 * @param array|string $data 数据
 * @param array $httpHeader 请求头部信息
 * @param string $method 方式
 * @param int $timeout 超时时间
 * @param bool $jsonMark true 数组转JSON，false 不做转换
 * @return string
 */
function curl($url, $data, $httpHeader = array('Content-Type: application/json'), $method = 'POST', $timeout = 10 , $jsonMark = true)
{
    $ch = curl_init();
    $timeout = $timeout <= 0 ? 300 : $timeout;
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    if (strtoupper($method) == 'POST') {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        if(is_array($data) && $jsonMark){
            $data=json_encode($data);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    } else {
        curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data));
    }
    if (!empty($httpHeader) && is_array($httpHeader)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $response = curl_exec($ch);
    $http = curl_getinfo($ch);
    $error=curl_error($ch);
    curl_close($ch);
    if(empty($error)&&$http['http_code']==200){
        return $response;
    }else{
        if($timeout!=1){
            trace("http_code:{$http['http_code']} error:{$error}",'error');
        }
        return $response;
    }
}
//多线程curl请求
function curlMulti($url,$data,$timeout=300)
{
    $res = array();
    $mh = curl_multi_init();//创建多个curl语柄
    $count=count($data);
    for ($k = 0; $k < $count; $k++) {
        $json = json_encode($data[$k]);
        $conn[$k] = curl_init($url);
        curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);//设置超时时间
        curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($conn[$k], CURLOPT_MAXREDIRS, 7);//HTTp定向级别
        curl_setopt($conn[$k], CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
        curl_setopt($conn[$k], CURLOPT_SSL_VERIFYHOST, 0); // 不检查证书中是否设置域名
        curl_setopt($conn[$k], CURLOPT_SSL_VERIFYPEER, false);// 跳过证书检查
        curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回，而不是直接输出
        curl_setopt($conn[$k], CURLOPT_HEADER, false);//启用时会将头文件的信息作为数据流输出
        curl_setopt($conn[$k], CURLOPT_POSTFIELDS, $json);
        curl_setopt($conn[$k], CURLOPT_POST, 1);
        curl_setopt($conn[$k], CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_multi_add_handle($mh, $conn[$k]);
    }
    // 执行批处理句柄
    $active = null;
    $data=[];
    do {
        curl_multi_exec($mh, $active);//当无数据，active=true
    } while ($active >0);//当正在接受数据时
    for ($k = 0; $k < $count; $k++) {
        $http = curl_getinfo($conn[$k]);
        $error=curl_error($conn[$k]);
        if(empty($error)&&$http['http_code']==200){
            $res[$k] = curl_multi_getcontent($conn[$k]);//获得返回信息
            $v1=json_decode($res[$k],true);
            if(is_array($v1)&&isset($v1['state'])&&$v1['state'] == '000001'&&!empty($v1['data'])){
                $data=array_merge($data,$v1['data']);
            }
        }else{
            trace("http_code:{$http['http_code']} error:{$error}",'error');
        }
        curl_multi_remove_handle($mh, $conn[$k]);//释放资源
        curl_close($conn[$k]);//关闭语柄
    }
    curl_multi_close($mh);
    return $data;
}
/**
 * 获取毫秒
 * @param date $datetime 日期，如：2018-11-07
 * @return string
 */
function get_millitime($datetime)
{
    $time = strtotime($datetime);
    if ($time <= 0) {
        return '0';
    }
    $time .= '000';
    return $time;
}
//如果时间没有返回空
function changeTime($datetime)
{
    if($datetime=='1970-01-01 00:00:00'||$datetime=='0000-00-00 00:00:00'){
        return '';
    }
    return $datetime;
}
//检验sku格式
function checkSku($str)
{
    $str=trim($str);
    if (preg_match('/^[A-Z][A-Z0-9]{2,14}$/', $str)) {
        return true;
    } else {
        return false;
    }
}
//校验参数
function checkParam($param,$rule,$key=''){
    if(!empty($key)){
        if(!isset($param[$key])){
            returnJson('000501',"{$key}不存在");
        }
        $val=$param[$key];
    }else{
        $val=$param;
    }
    foreach ($rule as $v){
        if($v=='e'){
            if(empty($val)){
                returnJson('000501',"{$key}参数不能为空");
            }
        }
        if($v=='a'){
            if(!is_array($val)){
                returnJson('000501',"{$key}参数必须为数组");
            }
        }
        if($v=='i'){
            if (!preg_match('/^[1-9][0-9]*$/', $val)) {
                returnJson('000501',"{$key}参数必须为正整数");
            }
        }
    }
    return $val;
}
//处理接口返回
function curlData($res,$log=''){
    $json = json_decode($res, true);
    if(is_array($json)&&isset($json['state'])&&$json['state'] !== '000001'){
        $backtrace=debug_backtrace(false, 1);
        $file='';
        if(!empty($backtrace[0])&&!empty($backtrace[0]['file'])&&!empty($backtrace[0]['line'])){
            $file="file=>  {$backtrace[0]['file']}   line=>  {$backtrace[0]['line']} ".PHP_EOL;
        }
         \think\Log::record($file.$res.$log, 'error');
        return [];
    }else{
        if(isset($json['data'])){
            return $json['data'];
        }else{
            return [];
        }
    }
}
//按逗号 空格 切割字段 返回数组
function explodeStr($str){
    return array_values(array_filter(explode(',', preg_replace("/(\r\n)|(\n)|(\s)|(\t)|(，)/", ',', $str))));
}
/**
 * 二维数组根据某个字段排序
 * @param array $array 要排序的数组
 * @param string $keys   要排序的键字段
 * @param string $sort  排序类型  SORT_ASC     SORT_DESC 
 * @return array 排序后的数组
 */
function arraySort($array, $keys, $sort = SORT_DESC) {
    $keysValue = [];
    foreach ($array as $k => $v) {
        $keysValue[$k] = $v[$keys];
    }
    array_multisort($keysValue, $sort, $array);
    return $array;
}